import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_first_one/custom_behavior/keep_alive_warpper.dart';
import '../pages/home/home.dart';
import '../pages/story/story.dart';
import '../pages/mike/mike.dart';
import '../pages/create/create.dart';
import '../pages/me/me.dart';

class MyTabBar extends StatefulWidget {
  const MyTabBar({super.key});

  @override
  // ignore: library_private_types_in_public_api
  _MyTabBarState createState() => _MyTabBarState();
}

class _MyTabBarState extends State<MyTabBar> {
  int curIndex = 2;
  final List<Widget> pages = [
    const KeepAliveWrapper(child: Home()),
    const KeepAliveWrapper(child: Story()),
    const KeepAliveWrapper(child: Mike()),
    const KeepAliveWrapper(child: Create()),
    const KeepAliveWrapper(child: Me())
  ];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  // pageview的页面控制器（显示页面，是否缓存等）
  final PageController _pageController = PageController(initialPage: 2);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // 利用PageView来切换并且缓存页面
      body: PageView(
        physics: const NeverScrollableScrollPhysics(),
        // 控制器，控制器当前显示的页面
        controller: _pageController,
        // 页面改变执行的方法
        onPageChanged: (value) => {
          setState(() {
            // 更新当前页面的下标
            curIndex = value;
          })
        },
        children: pages,
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        unselectedFontSize: 10.0,
        selectedFontSize: 10.0,
        currentIndex: curIndex,
        items: [
          const BottomNavigationBarItem(icon: Icon(Icons.home), label: '推荐'),
          const BottomNavigationBarItem(icon: Icon(Icons.home), label: '故事'),
          BottomNavigationBarItem(
              // 绑定长按事件
              icon: GestureDetector(
                  child: Icon(Icons.mic),
                  onLongPressStart: (details) {
                    if (curIndex != 2) return;
                    // 触发mike页面方法
                    Mike().methodToTrigger(1);
                  },
                  onLongPressMoveUpdate: (details) {
                    if (curIndex != 2) return;
                    Mike().methodToTrigger(2, [
                      details.globalPosition.dx.toInt(),
                      details.globalPosition.dy.toInt()
                    ]);
                  },
                  onLongPressEnd: (details) {
                    if (curIndex != 2) return;
                    // 触发mike页面方法
                    Mike().methodToTrigger(3);
                  }),
              label: '播放'),
          const BottomNavigationBarItem(icon: Icon(Icons.home), label: '创作'),
          const BottomNavigationBarItem(icon: Icon(Icons.people), label: '我的'),
        ],
        fixedColor: Colors.blue,
        onTap: (value) {
          setState(() {
            curIndex = value;
            // 跳转页面
            _pageController.jumpToPage(curIndex);
          });
        },
      ),
    );
  }
}
